package org.lalib.foundation;

import java.io.Serializable;

import org.hibernate.SessionFactory;
import org.lalib.acquire.IFDataAccess;
import org.lalib.entity.object.EBaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class FDataAccess<T extends EBaseEntity> extends HibernateDaoSupport implements IFDataAccess<T>, Serializable {

	private static final long serialVersionUID = 1L;

	protected abstract Class<T> getClassType();

	@Autowired
	public void setFactory(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	@Override
	public final void insert(T value) {
		getHibernateTemplate().save(value);
		getHibernateTemplate().flush();
	}

	@Override
	public final void update(T value) {
		getHibernateTemplate().saveOrUpdate(value);
		getHibernateTemplate().flush();
	}

	@Override
	public final void remove(T value) {
		getHibernateTemplate().delete(value);
		getHibernateTemplate().flush();
	}

	@Override
	public final T getEntityById(Long id) {
		return (T) getHibernateTemplate().get(getClassType(), id);
	}

	public final T loadEntity(Long id) {
		return getHibernateTemplate().load(getClassType(), id);
	}
}